package com.bulletelc.apollopre.module.main.center.orders.detail;

import android.util.Log;

import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.OrderDetailBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.bulletelc.apollopre.utils.MyUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author: lhl
 * date  : 2018/01/24
 * desc  : TODO
 */

public class OrderDetailPresenter extends BasePresenter<OrderDetailContract.View> implements OrderDetailContract.Presenter {

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getOrderDetail(String orderId) {
        getOrderDetailImp(orderId);
    }

    @Override
    public void confirmSign(String json) {
        confirmSignImp(json);
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetailImp(String orderId) {
        Log.i("sssssss", "getOrderDetailImp: "+  MyUtil.getAuthToken());
        OkGo.<String>post(NetWorkPath.POST_ORDER_DETAIL+"?id="+orderId)
                .headers(NetWorkPath.HEADER_KEY_AUTH, MyUtil.getAuthToken())
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(Response<String> response) {
//                        Log.i("78945", response.body().toString());
                        parseOrderDetail(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onError(response.getException().toString());
                        }
                    }
                });

    }

    /**
     * 解析订单详情数据
     */
    private void parseOrderDetail(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject metaObj = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(metaObj.getString("code"))) {
                    Gson gson = new Gson();
                    OrderDetailBean orderDetailBean = gson.fromJson(object.getJSONObject("data").toString(),OrderDetailBean.class);
                    if (isViewAttached()) {
                        getView().onSuccess(orderDetailBean);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFail(metaObj.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onError("数据解析有误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFail(response.body().toString());
            }
        }
    }

    /**
     * 确定签收
     * @param json
     */
    private void confirmSignImp(String json) {
        RequestBody body=RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON),json);
        OkGo.<String>post(NetWorkPath.POST_ORDER_STATE_UPDATE)
                .headers(NetWorkPath.HEADER_KEY_AUTH,MyUtil.getAuthToken())
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseConfirmSign(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorConfirmSign(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析确认签收的数据
     * @param response
     */
    private void parseConfirmSign(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                org.json.JSONObject object = new org.json.JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(object.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    if (isViewAttached()) {
                        getView().onSuccessConfirmSign();
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailConfirmSign(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorConfirmSign("数据解释错误");
                }
            }
        } else {
            if (isViewAttached()) {
                getView().onFailConfirmSign(response.body().toString());
            }
        }
    }

}
